import { Checksum, Hash } from "@smithy/types";
import { toUint8Array } from "@smithy/util-utf8";
import { stream } from "@kit.ArkTS";

// import { Writable, WritableOptions } from "stream";

/**
 * @internal
 */
export class HashCalculator extends stream.Writable {
  constructor(
    public readonly hash: Checksum | Hash
  ) {
    super();
  }

  doWrite(chunk: string | Uint8Array, encoding: string, callback: Function): void {
    try {
      this.hash.update(toUint8Array(chunk));
    } catch (err) {
      return callback(err);
    }
    callback();
  }
}
